//
//  ViewController.m
//  UIHomeWork14_礼物说分类攻略界面
//
//  Created by tusm on 16/8/2.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "ViewController.h"

#import "LBCollectionReusableView.h"
#import "ILCollectionReusableView.h"
#import "LBCollectionViewCell.h"
#define kCollectionVeiwCellReuse @"reuseCell"
#define kCollectionViewHeaderReuse @"headerReuse"
#define kCollectionViewHeaderCollectionView @"headerCollection"

@interface ViewController () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTitleView];
    [self createCollectionView];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.99 green:0.19 blue:0.30 alpha:1.00];
}
- (void)createTitleView {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 150, 40)];
    //    view.backgroundColor = [UIColor colorWithRed:0.99 green:0.19 blue:0.30 alpha:1.00];
    self.navigationItem.titleView = view;
    UILabel *labelLeft = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 70, 40)];
    labelLeft.text = @"攻略";
    labelLeft.textAlignment = NSTextAlignmentCenter;
    labelLeft.font = [UIFont systemFontOfSize:20];
    labelLeft.textColor = [UIColor whiteColor];
    UILabel *labelRight = [[UILabel alloc] initWithFrame:CGRectMake(80, 0, 70, 40)];
    labelRight.text = @"单品";
    labelRight.textAlignment = NSTextAlignmentCenter;
    labelRight.font = [UIFont systemFontOfSize:20];
    labelRight.textColor = [UIColor whiteColor];
    
    [view addSubview:labelLeft];
    [view addSubview:labelRight];
}
- (void)createCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 30) / 2, 100);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    layout.headerReferenceSize = CGSizeMake(0, 50);
    UICollectionView *collectionVeiw = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionVeiw.backgroundColor = [UIColor whiteColor];
    collectionVeiw.delegate = self;
    collectionVeiw.dataSource = self;
    collectionVeiw.showsVerticalScrollIndicator = NO;
    [self.view addSubview:collectionVeiw];
    // cell
    [collectionVeiw registerClass:[LBCollectionViewCell class] forCellWithReuseIdentifier:kCollectionVeiwCellReuse];
    // header
    [collectionVeiw registerClass:[LBCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderReuse];
    // headerCollectionView
    [collectionVeiw registerClass:[ILCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderCollectionView];
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        ILCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderCollectionView forIndexPath:indexPath];
        return view;
    } else {
        LBCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHeaderReuse forIndexPath:indexPath];
        return view;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 300);
    } else {
        return CGSizeMake(0, 50);
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 30;
    }
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LBCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionVeiwCellReuse forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0f  green:arc4random() % 256 / 255.0f blue:arc4random() % 256 / 255.0f alpha:1];
    return cell;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
